package com.common.tools.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 *
 * @author lxw
 */
public class MultipartFileUtils {

    public static File save(MultipartFile file, String wordDir) throws IOException {
        String inputFile = wordDir + File.separator + "input" + File.separator + DateUtil.today() + File.separator + UUID.randomUUID() + "." + getFileType(Objects.requireNonNull(file.getOriginalFilename()));
        return FileUtil.writeFromStream(file.getInputStream(), inputFile);
    }

    public static String getFileType(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
}
